package com.example.leetcode.leetcode.concurrent;


/**
 * @author zhuhaitao
 * @email zhuhaitao@imoran.net
 * @date 2020/8/4 11:56 上午
 */
public class MyRunnable {


    public static void main(String[] args) {
        Spinlock spinlock = new Spinlock();
        Runnable t1 = () -> {
            spinlock.lock();
            System.out.println(Thread.currentThread().getName()+"加锁");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                spinlock.unlock();
                System.out.println(Thread.currentThread().getName()+"释放锁");
            }
        };
        Runnable t2 = () -> {
            spinlock.lock();
            System.out.println(Thread.currentThread().getName()+"加锁");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                spinlock.unlock();
                System.out.println(Thread.currentThread().getName()+"释放锁");
            }
        };

        Thread thread1=new Thread(t1,"线程1");
        Thread thread2=new Thread(t2,"线程2");
        thread1.start();
        thread2.start();
    }
}
